package com.redwood.report.activity.bak;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.redwood.report.common.Constant;
import com.redwood.report.R;
import com.redwood.report.util.ImageLoaderUtil;
import com.redwood.report.util.actionbar.LightStatusBarUtils;
import com.redwood.report.util.actionbar.ToolbarUtil;
import com.redwood.report.widget.PreviewViewPager;

import butterknife.BindView;
import butterknife.ButterKnife;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * 预览列表
 */
public class PicturePreviewListActivity extends AppCompatActivity {
    @BindView(R.id.tv_center)
    TextView tvCenter;
//    @BindView(R.id.iv_seleted)
//    ImageView ivSeleted;
    @BindView(R.id.preview_pager)
    PreviewViewPager previewPager;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    private PreviewViewPager viewPager;
    private int position = 0;
    private SimpleFragmentAdapter adapter;
    private LayoutInflater inflater;
    private String[] urls;
   // private List<String> titles;
    private int selectPostion = -1;
    private int nowPage = 0;

    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //标题栏颜色
        ToolbarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.black));
        //深色主题 true字颜色为深色 false 为白色标题字
        LightStatusBarUtils.setLightStatusBar(this, true);
        setContentView(R.layout.activity_picture_preview_list);
        urls = getIntent().getStringArrayExtra("urls");
     //   titles = (List<String>) getIntent().getSerializableExtra("titles");
        position = getIntent().getIntExtra("position", 0);
        nowPage = position;
        selectPostion = getIntent().getIntExtra("selectPostion", -1);
        inflater = LayoutInflater.from(this);
        ButterKnife.bind(this);
        initView();
        initViewPageAdapterData();
    }

    private void initView() {
       // if (selectPostion == nowPage) {
           // ivSeleted.setImageResource(R.drawable.btn_gou2);
     //   }
      //  tvCenter.setText(titles.get(position));
        viewPager = (PreviewViewPager) findViewById(R.id.preview_pager);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goback();
            }
        });
    }

    private void goback() {
        Intent intent = new Intent();
        if (selectPostion != -1) {
            intent.putExtra("selectedPostion", selectPostion);
        }
        setResult(8859, intent);
        finish();
        overridePendingTransition(0, R.anim.a3);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {//点击的是返回键
            goback();
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    private void initViewPageAdapterData() {
        // tvCenter.setText();
        adapter = new SimpleFragmentAdapter();
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(position);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                // tvCenter.setText(position + 1 + "/" + partsFabricListBeanList.size()+"("+partsFabricListBeanList.get(position).getFabricCode()+")");
                nowPage = position;
           //     tvCenter.setText(titles.get(position));
               // ivSeleted.setImageResource(position == selectPostion ? R.drawable.btn_gou2 : R.drawable.btn_quan2);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

//    @OnClick(R.id.iv_seleted)
//    public void onViewClicked() {
//        if (selectPostion == nowPage) {
//            selectPostion = -1;
//            ivSeleted.setImageResource(R.drawable.btn_quan2);
//        } else {
//            selectPostion = nowPage;
//            ivSeleted.setImageResource(R.drawable.btn_gou2);
//        }
//    }


    public class SimpleFragmentAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return urls.length;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            (container).removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            View contentView = inflater.inflate(R.layout.activity_picture_preview, container, false);
            final PhotoView imageView = (PhotoView) contentView.findViewById(R.id.preview_image);

            String url = urls[position];
            ImageLoaderUtil.loadImage(PicturePreviewListActivity.this, Constant.getBaseUrl() + url, imageView);
            imageView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
                @Override
                public void onViewTap(View view, float x, float y) {
                    //finish();
                    //overridePendingTransition(0, R.anim.a3);
                    goback();

                }
            });
            (container).addView(contentView, 0);
            return contentView;
        }
    }
}
